
package com.dps.omc.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dps.omc.model.Handler;
import com.dps.omc.model.TemplateShell;
import com.dps.omc.model.User;
import com.dps.omc.service.IHandlerService;
import com.dps.omc.service.ITemplateShellService;
import com.dps.omc.model.TemplateHandler;
import com.dps.omc.service.ITemplateHandlerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dps.common.domain.view.BizData4Page;
import com.dps.common.utils.IdGenerator;
import com.dps.common.utils.JsonResult;
import com.dps.common.utils.PageParam;
import com.dps.common.utils.Constants;

@Controller
public class TemplateHandlerAction extends AbstractAdminController<ITemplateHandlerService>{

    @Autowired
    private ITemplateHandlerService templateHandlerService;
    @Autowired
    private ITemplateShellService templateShellService;
    @Autowired
    private IHandlerService handlerService;
    
    @RequestMapping(value="/templatehandler/list")
    public String list(TemplateHandler templateHandler, ModelMap model, PageParam pageParam, HttpServletRequest request, HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(templateHandler); 	
    	BizData4Page<TemplateHandler> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("templateHandler", templateHandler);
    	return "/module/templatehandler/list";
    }    
    
    @RequestMapping(value="/templatehandler/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

        List<TemplateShell> templateShells = templateShellService.findAll();
        List<Handler>  handlers = handlerService.findAll();
        model.addAttribute("templateShells", templateShells);
        model.addAttribute("handlers", handlers);
    	model.addAttribute("templateHandler", new TemplateHandler());
        return "module/templatehandler/edit";
    }    
    
    @RequestMapping(value="/templatehandler/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	TemplateHandler templateHandler = templateHandlerService.fetch(id);
        List<TemplateShell> templateShells = templateShellService.findAll();
        List<Handler>  handlers = handlerService.findAll();
        model.addAttribute("templateShells", templateShells);
        model.addAttribute("handlers", handlers);
    	model.addAttribute("templateHandler", templateHandler);
        return "module/templatehandler/edit";
    }    
    
    @RequestMapping(value="/templatehandler/save")
    @ResponseBody
    public String save(HttpServletRequest request,TemplateHandler templateHandler){
		boolean isAddNew = StringUtils.isBlank(templateHandler.getId())?true:false;
        User user=getCurrentUser();
		if(isAddNew)
		{
			templateHandler.setId(IdGenerator.createNewId());
            templateHandler.setCreaterId(user.getId());
            templateHandler.setCreaterName(user.getRealName());
			templateHandlerService.add(templateHandler);
		}
		else
		{
			templateHandlerService.update(templateHandler);
		}
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/templatehandler/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	templateHandlerService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }       
	
    protected Map getQueryMap(TemplateHandler templateHandler)
    {
    	Map<String, Object> conditions = new HashMap();
    	
		conditions.put("templateId", templateHandler.getTemplateId());		
		conditions.put("clazz", templateHandler.getClazz());		
		conditions.put("seq", templateHandler.getSeq());		
		conditions.put("createrId", templateHandler.getCreaterId());		
		conditions.put("createrName", templateHandler.getCreaterName());		
		conditions.put("createDate", templateHandler.getCreateDate());		
    	return conditions;
    }

    @Override
    protected ITemplateHandlerService getMainService() {
        return templateHandlerService;
    }

    @Override
    protected String getMainObjName() {
        return "templatehandler";
    }

    @Override
    protected String getViewTitle() {
        return "templatehandler";
    }
}
